package com.bigflexlib.async
{

	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;


	public class URLLoaderDeferred extends Deferred
	{
		private var loader:URLLoader;

		public function URLLoaderDeferred(target:URLLoader)
		{
			super(target.close);
			loader = target;

			loader.addEventListener(Event.COMPLETE, loader_complete);
			loader.addEventListener(IOErrorEvent.IO_ERROR, loader_ioError);
			loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, loader_securityError);
			// Note that this wrapper intentionally doesn't bind itself to the
			// HTTP_STATUS event. It doesn't make sense, as the HTTP_STTATUS
			// event is always followed by a complete or error event.
		}

		private function loader_complete(event:Event):void
		{
			callback(loader.data);
		}

		private function loader_ioError(event:IOErrorEvent):void
		{
			errback(event);
		}

		private function loader_securityError(event:SecurityErrorEvent):void
		{
			errback(event);
		}
	}
}